package com.lill.product.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.product.model.ProductQualifierModel;
import com.lill.product.service.IProductQualifierService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.model.SysUserEntity;

/**
 * 合格证Controller
 *
 * @author lill
 * @date 2021-11-14 00:40:06
 */
@RestController
@RequestMapping("product/qualifier")
public class ProductQualifierController {
    @Autowired
    private IProductQualifierService productQualifierService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	params.put("companyId", user.getCompanyId());
		params.put("branchId", user.getBranchId());
        //查询列表数据
        Query query = new Query(params);
        List<ProductQualifierModel> productQualifierList = productQualifierService.queryList(query);
        int total = productQualifierService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(productQualifierList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
    	ProductQualifierModel productQualifier = productQualifierService.queryObject(id);
        return R.ok().put("productQualifier", productQualifier);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ProductQualifierModel productQualifier,@CurrentUser SysUserEntity user) {
    	productQualifier.setCompanyId(user.getCompanyId());
    	productQualifier.setBranchId(user.getBranchId());
    	productQualifier.setStatus(1);
    	productQualifier.setCreateUser(user.getId());
    	productQualifier.setCreateTime(new Date());
        int result=productQualifierService.save(productQualifier);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ProductQualifierModel productQualifier,@CurrentUser SysUserEntity user) {
    	productQualifier.setCompanyId(user.getCompanyId());
    	productQualifier.setBranchId(user.getBranchId());
    	productQualifier.setUpdateUser(user.getId());
    	productQualifier.setUpdateTime(new Date());
        int result=productQualifierService.update(productQualifier);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
        int result=productQualifierService.deleteLogicByMap(params);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	params.put("companyId", user.getCompanyId());
		params.put("branchId", user.getBranchId());
		StringBuffer qualifierBuffer=new StringBuffer("");
        List<ProductQualifierModel> list = productQualifierService.queryList(params);
        if(CollectionUtils.isNotEmpty(list)){
        	for (ProductQualifierModel qualifier : list) {
				if(qualifierBuffer.length()>0){
					qualifierBuffer.append(";");
				}
				qualifierBuffer.append(qualifier.getId()).append(":").append(qualifier.getName());
			}
        }
        return R.ok().put("list", list).put("dict",qualifierBuffer.toString());
    }
}
